/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Feb 15, 2012  8:58:39 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : apps.promis.in.common
 * File Name : CostArticleUmConversionAction.java
 * TODO
 */
package apps.promis.in.common;

import java.util.List;

import jpa.entitys.promis.common.CostArticleUmConversion;
import jpa.entitys.promis.common.CostArticleUmConversionPK;
import jpa.entitys.synchro.staging.MmConvFactInb;
import jpa.service.promis.common.CostArticleUmConversionServices;
import jpa.service.synchro.staging.MmConvFactInbServices;

import org.apache.log4j.Logger;

import promis.utility.CommonService;
import promis.utility.Utilitys;

import apps.sessionFactory;

/**
 * 
 * @author Chatree Siangklom of Administrator
 * Date =Nov 13, 2013 : 2:35:25 PM 
 * CreateBy=Chatree Siangklom
 * 
 * CostArticleUmConversionAction
 * TODO
 */
public class CostArticleUmConversionAction {
	
	private static final Logger log = Logger.getLogger(CostArticleUmConversionAction.class);
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:35:30 PM 
	 * CreateBy=Chatree Siangklom
	 * CostArticleUmConversionAction
	 * TODO
	 */
	public CostArticleUmConversionAction() {
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:35:34 PM 
	 * CreateBy=Chatree Siangklom
	 * CostArticleUmConversion
	 * TODO
	 * @param um
	 * @param masterUm
	 * @param articleCode
	 * @return
	 * @throws Exception
	 */
	public CostArticleUmConversion findByKey(String um, String masterUm, String articleCode) throws Exception{
		CostArticleUmConversionServices costArticleUmConversionSrv =  new CostArticleUmConversionServices(sessionFactory.CreateEmPromis());
		CostArticleUmConversionPK costArticleUmConversionPK = new CostArticleUmConversionPK();
		costArticleUmConversionPK.setMasterUm(masterUm);
		costArticleUmConversionPK.setUm(um);
		costArticleUmConversionPK.setArticleCode(articleCode);
		
		return costArticleUmConversionSrv.findById(costArticleUmConversionPK);
	}
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:35:36 PM 
	 * CreateBy=Chatree Siangklom
	 * CostArticleUmConversion
	 * TODO
	 * @param mmConvFactInb
	 * @return
	 * @throws Exception
	 */
	public CostArticleUmConversion SaveOrUpdated(MmConvFactInb mmConvFactInb) throws Exception {
		
		CostArticleUmConversionServices costArticleUmConversionSrv =  new CostArticleUmConversionServices(sessionFactory.CreateEmPromis());
		CostArticleUmConversion model = findByKey(mmConvFactInb.getUm(), mmConvFactInb.getMasterUm(), mmConvFactInb.getArticleCode());
		
		if(model == null)
		{
			model = new CostArticleUmConversion();
			
			//Set Id
			CostArticleUmConversionPK costArticleUmConversionPK = new CostArticleUmConversionPK();
			costArticleUmConversionPK.setMasterUm(mmConvFactInb.getMasterUm());
			costArticleUmConversionPK.setUm(mmConvFactInb.getUm());
			costArticleUmConversionPK.setArticleCode(mmConvFactInb.getArticleCode());
			model.setId(costArticleUmConversionPK);
			
			model.setNumeratorConversion(mmConvFactInb.getNumeratorConversion());
			model.setDenominatorConversion(mmConvFactInb.getDenominatorConversion());
			
			String temp = CommonService.IsNullRString(mmConvFactInb.getNumeratorConversion()+"") + " " 
						+ CommonService.IsNullRString(mmConvFactInb.getUm()) + " = " 
						+ CommonService.IsNullRString(mmConvFactInb.getDenominatorConversion()+"") + " "
						+ CommonService.IsNullRString(mmConvFactInb.getMasterUm());
			model.setDescription(temp);
			
			model.setSyncExternalRefKey(mmConvFactInb.getExtRefKey());
			model.setSyncExternalRefNo(mmConvFactInb.getExtRefNo());
			model.setSyncIssync("N");
			
			model.setCreatedBy(mmConvFactInb.getCreatedBy());
			model.setCreatedOn(Utilitys.ConvertStringToDateTime(mmConvFactInb.getCreatedOn()));
			model.setLastUpdBy(mmConvFactInb.getUpdatedBy());
			model.setLastUpdOn(CommonService.ConvertStringToDateTime(mmConvFactInb.getUpdatedOn()));
			
			if(!CommonService.IsNull(mmConvFactInb.getUpdatedBy())){
				model.setLastUpdBy(mmConvFactInb.getCreatedBy());
				model.setLastUpdOn(CommonService.ConvertStringToDateTime(mmConvFactInb.getCreatedOn()));
			}
		}
		else
		{
			model.setNumeratorConversion(mmConvFactInb.getNumeratorConversion());
			model.setDenominatorConversion(mmConvFactInb.getDenominatorConversion());
			
			String temp = CommonService.IsNullRString(mmConvFactInb.getDenominatorConversion()+"") + " " + CommonService.IsNullRString(mmConvFactInb.getUm()) + " = " + CommonService.IsNullRString(mmConvFactInb.getNumeratorConversion()+"") + " " + CommonService.IsNullRString(mmConvFactInb.getMasterUm());
			model.setDescription(temp);			
			model.setSyncExternalRefKey(mmConvFactInb.getExtRefKey());
			model.setSyncExternalRefNo(mmConvFactInb.getExtRefNo());			
			model.setSyncIssync("N");			
			
			if(CommonService.IsNull(mmConvFactInb.getUpdatedOn()))
				model.setLastUpdOn(Utilitys.ConvertStringToDateTime(mmConvFactInb.getCreatedOn()));				
			else
				model.setLastUpdOn(Utilitys.ConvertStringToDateTime(mmConvFactInb.getUpdatedOn()));
			
			model.setLastUpdBy(CommonService.IsNullRNull(mmConvFactInb.getCreatedBy()));
		}		
		return costArticleUmConversionSrv.InsertedOrUpdated(model);
	}
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:35:41 PM 
	 * CreateBy=Chatree Siangklom
	 * void
	 * TODO
	 * @param lst
	 * @throws Exception
	 */
	public void Start(List<MmConvFactInb> lst) throws Exception {		
		try
		{
			MmConvFactInbServices mmConvFactInbServ = new MmConvFactInbServices(sessionFactory.CreateEmSynchro());
			//List<MmConvFactInb> lst = mmConvFactInbServ.findByIsSync("N");
			UmMasterAction umMaster = new UmMasterAction();
			if(!CommonService.IsNull(lst))
			{
				for(MmConvFactInb mmConvFactInb : lst){
					try{
						
						log.info("mmConvFactInb.getPromisStagingId : " + mmConvFactInb.getPromisStagingId());
						System.out.println("mmConvFactInb.getPromisStagingId : " + mmConvFactInb.getPromisStagingId());
						
						umMaster.InsertOrUpdatePromisUmMaster(mmConvFactInb);
						
						SaveOrUpdated(mmConvFactInb);					
						
						mmConvFactInb.setIssync("Y");
						mmConvFactInb.setMsgCode("S");
						mmConvFactInb.setMsgDesc("Success");
						mmConvFactInb.setMsgActResult(null);
						
						mmConvFactInbServ.Updated(mmConvFactInb);
						
					}catch(Exception e)
					{
						String tempValue = CommonService.CutString(e.getMessage(), 3500);
						mmConvFactInb.setIssync("Y");
						mmConvFactInb.setMsgCode("E");
						mmConvFactInb.setMsgDesc(tempValue);
						mmConvFactInb.setMsgActResult(tempValue);
						
						mmConvFactInbServ.Updated(mmConvFactInb);
						log.error(e);
					}				
				}
			}else
				log.info("Conversion List is null ");
				
		}catch(Exception e){
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:35:47 PM 
	 * CreateBy=Chatree Siangklom
	 * void
	 * TODO
	 * @throws Exception
	 */
	@Deprecated
	private void Tested() throws Exception{
		
		MmConvFactInbServices mmConvFactInbServ = new MmConvFactInbServices(sessionFactory.CreateEmSynchro());
		List<MmConvFactInb> lst = mmConvFactInbServ.findByIsSync("N");
		UmMasterAction umMaster = new UmMasterAction();
		if(!CommonService.IsNull(lst)){
			for(MmConvFactInb mmConvFactInb : lst){
				try{
					
					log.info("mmConvFactInb.getPromisStagingId : " + mmConvFactInb.getPromisStagingId());
					
					umMaster.InsertOrUpdatePromisUmMaster(mmConvFactInb);
					SaveOrUpdated(mmConvFactInb);					
					
					mmConvFactInb.setIssync("Y");
					mmConvFactInb.setMsgCode("S");
					mmConvFactInb.setMsgDesc("Success");
					mmConvFactInb.setMsgActResult(null);
					mmConvFactInbServ.Updated(mmConvFactInb);
					
				}catch(Exception e)
				{
					mmConvFactInb.setIssync("N");
					mmConvFactInb.setMsgCode("E");
					mmConvFactInb.setMsgDesc(CommonService.CutString(e.getMessage(), 3500));
					mmConvFactInb.setMsgActResult( CommonService.CutString(e.getMessage(), 3500));
					mmConvFactInbServ.Updated(mmConvFactInb);
					log.error(e);
				}				
			}
		}
	}

//	/**
//	 * @param args
//	 */
//	public static void main(String[] args) {
//		// TODO Auto-generated method stub
//		
//		CostArticleUmConversionAction xx = new CostArticleUmConversionAction();
//		try {
//			
//			xx.Tested();
//			
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}

}
